package cn.com.wblog.dao;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.lang.Files;
import org.nutz.mvc.upload.FieldMeta;
import org.nutz.mvc.upload.TempFile;

import cn.com.wblog.pojo.ArticleType;
import cn.com.wblog.pojo.Picture;
import cn.com.wblog.pojo.PictureType;
import cn.com.wblog.utils.Utils;

public class PictureTypeDao extends AbstractDao {
	
	private final static String PHOTO_ATTACHMENT_DIR = "photo" ;
	
	public PictureTypeDao(Dao dao){
		this.dao = dao;
	}
	
	public List<PictureType> getList(int currentPage, int numPerPage, String keyword) {
		List<PictureType> list = this.dao.query(PictureType.class, Cnd.where("name", "LIKE", "%"+(keyword==null?"":keyword)+"%"), dao.createPager(currentPage, numPerPage));
		for (PictureType pictureType : list) {
			this.dao.fetchLinks(pictureType, "user");
		}
		return list;
	}
	
	public List<PictureType> getType(int userId){
		List<PictureType> list = this.dao.query(PictureType.class, Cnd.where("userId","=",""+userId+""), null);
		return list;
	}
	
	public void update(PictureType pictureType){
		this.dao.update(pictureType);
	}
	
	public void insert(PictureType pictureType){
		this.dao.insert(pictureType);
	}
	
	public void deleteById(int id) {
		PictureType pictureType = this.dao.fetch(PictureType.class, id);
		List<Picture> pictures = this.dao.query(Picture.class, Cnd.where("typeId", "=", id), null);
		this.dao.deleteWith(pictureType , "pictures");
	}
	
	public String saveAttachment(File file, String realPath) {
		String dirPath = realPath + PHOTO_ATTACHMENT_DIR;
		File dir = new File(dirPath);
		if (!dir.exists()) {
			dir.mkdir();
		}
		String oldFileName = file.getName();
		String extension = oldFileName.substring(oldFileName.lastIndexOf("."));
		String newFileName = Utils.renameFile() + extension;
		File newFile = new File(dir, newFileName);
		try {
			Files.move(file, newFile);
		} catch (IOException ioex) {
			Logger.getLogger(Picture.class.getName());
		}
		String path = "/wblog/" + PictureTypeDao.PHOTO_ATTACHMENT_DIR + "/" + newFileName;
		return path;
	}
	
	public void insertPicture(Picture picture){
		this.dao.insert(picture);
	}
	
	public String readPicture(Picture picture){
		String path = picture.getUrl();
		return path;
	}
	
	public int count(String keyword) {
		return this.dao.count(PictureType.class, Cnd.where("name", "LIKE", "%"+(keyword==null?"":keyword)+"%"));
	}
	
	public List<Picture> getPicture(int typeId){
		List<Picture> list = this.dao.query(Picture.class,Cnd.where("typeId", "==", typeId),null);
		return list;
	}
	
	public List<Picture> getAllPhoto(int typeId) {
		List<Picture> list = this.dao.query(Picture.class, Cnd.where("typeId", "=", typeId), null);
		for (Picture picture : list) {
			this.dao.fetchLinks(picture, "pictureType");
		}
		return list;
	}
	
	public PictureType getPictureType(int typeId) {
		return this.dao.fetch(PictureType.class, typeId);
	}
	
	public void deletePhoto(int id) {
		this.dao.delete(Picture.class, id);
	}
	
/*	
	public void update(Picture picture){
		this.dao.update(picture);
	}
*/	
}
